// pages/my/order/order.js
import {tokenrequest} from "../../../request/index.js";
var util = require('../../../utils/util.js');
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{ name: '已完成', index: 0 }, { name: '待付款', index: 1 }, { name: '已取消', index: 2 }],
    total:0,
    //完成订单信息
    finishOrder:[],
    finishtime:[],
    finishtotal:null,
    //待支付订单信息
    payOrder:[],
    paytime:[],
    paytotal:0,
    //已取消订单信息
    cancleOrder:[],
    cancletime:[],
    cancletotal:0,
  },
  QueryParams:{
    pagenum:1,
    pagesize:4
  },
  finishtotalpages:1,
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const token = wx.getStorageSync("token");
    var that = this;
    if (!token) {
    console.log("token不存在");
     wx.reLaunch({
      url: '/pages/auth/auth',
     })
     wx.showToast({
      title: '请先登陆授权',
    })
     return;
   }else{
    console.log("如果token存在");
    wx.request({
      url: 'http://localhost:8080/ipark/user/login',
      data: {
        token: token
      },
      success: function (res) {
        console.log("前台token存在，校验token");
        //如果后台缓存失效了
        if (res.data.TokenExpired) {
          console.log("后台缓存已经过期了,小程序清空token和userinfo和openid");
          wx.removeStorageSync("token");
          wx.removeStorageSync("userInfo");
          wx.reLaunch({
            url: '/pages/auth/auth',
           })
           wx.showToast({
            title: '授权失效',
          })
           return;
        } else {
          console.log("后台缓存中的信息存在,直接返回");
          that.onPullDownRefresh();
        }
      }
    });
  }
  },
 /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    
  },
  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    var total = (this.data.currtab== 0 ? this.data.finishtotal : (this.data.currtab == 1 ? this.data.paytotal : this.data.canscletotal))
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current,
        total:total
      })
     
    }
  },
 
  tabChange: function (e) {
    this.setData({ currtab: e.detail.current });
    this.orderShow();
  },
 
 async orderShow() {
    let that = this;
    switch (this.data.currtab) {
      case 0:
        if(!wx.getStorageSync('finishOrder')){
          that.alreadyShow(0);
        }else{
          this.setData({
            total: wx.getStorageSync('finishtotal'),
            finishtime: wx.getStorageSync('finishtime'),
            finishOrder: wx.getStorageSync('finishOrder'),
          })
        }
        break;
      case 1:
        if(!wx.getStorageSync('payOrder')){
          that.alreadyShow(1);
        }else{
          this.setData({
            total: wx.getStorageSync('paytotal'),
            paytime: wx.getStorageSync('paytime'),
            payOrder: wx.getStorageSync('payOrder'),
          })
        }
        break;
      case 2:
        if(!wx.getStorageSync('cancleOrder')){
          that.alreadyShow(2);
        }else{
          this.setData({
            total: wx.getStorageSync('cancletotal'),
            cancletime: wx.getStorageSync('cancletime'),
            cancleOrder: wx.getStorageSync('cancleOrder'),
          })
        }
        break;
    }
  },
  async alreadyShow(status){
    var pagesize = this.QueryParams.pagesize;
    var pagenum = this.QueryParams.pagenum;
    const res=await tokenrequest({
      url: 'http://localhost:8080/ipark/order/historyorder',
      data:{
        status,
        pagesize,
        pagenum      
      },
    });
    var date = new Date(date);
    switch(this.data.currtab){
      case 0:
        const finishtotal=res.data.data.historyorders.total;
        this.totalPages=Math.ceil(finishtotal/this.QueryParams.pagesize);   
        let finishdateList = res.data.data.historyorders.pageOrderList.map(v=>v.odata);
        for(let c in finishdateList){
          let  date = util.formatTime(finishdateList[c])
          finishdateList[c] = date
        }
        wx.setStorageSync('finishOrder', [...this.data.finishOrder,...res.data.data.historyorders.pageOrderList]);
        wx.setStorageSync('finishtime', [...this.data.finishtime,...finishdateList]);
        wx.setStorageSync('finishtotal',this.data.finishOrder.length+res.data.data.historyorders.pageOrderList.length);
        this.setData({
          finishtotal: wx.getStorageSync('finishtotal'),
          finishtime: wx.getStorageSync('finishtime'),
          finishOrder: wx.getStorageSync('finishOrder'),
          total:wx.getStorageSync('finishtotal')
        })
        wx.stopPullDownRefresh();
        break;
      case 1:
        const paytotal=res.data.data.historyorders.total;
        console.log(res.data.data.historyorders.pageOrderList.length);
        this.totalPages=Math.ceil(paytotal/this.QueryParams.pagesize);   
        let paydateList = res.data.data.historyorders.pageOrderList.map(v=>v.odata);
        for(let c in paydateList){
          let  date = util.formatTime(paydateList[c])
          paydateList[c] = date
        }
        wx.setStorageSync('payOrder', [...this.data.payOrder,...res.data.data.historyorders.pageOrderList]);
        wx.setStorageSync('paytime', [...this.data.paytime,...paydateList]);
        wx.setStorageSync('paytotal',(this.data.payOrder.length+res.data.data.historyorders.pageOrderList.length));
        this.setData({
          paytotal: wx.getStorageSync('paytotal'),
          paytime: wx.getStorageSync('paytime'),
          payOrder: wx.getStorageSync('payOrder'),
        })
        wx.stopPullDownRefresh();
        break;
      case 2:
        const cancletotal=res.data.data.historyorders.total;
        this.totalPages=Math.ceil(cancletotal/this.QueryParams.pagesize);   
        let cancledateList = res.data.data.historyorders.pageOrderList.map(v=>v.odata);
        for(let c in cancledateList){
          let  date = util.formatTime(cancledateList[c])
          cancledateList[c] = date
        }
        wx.setStorageSync('cancleOrder', [...this.data.cancleOrder,...res.data.data.historyorders.pageOrderList]);
        wx.setStorageSync('cancletime', [...this.data.cancletime,...cancledateList]);
        wx.setStorageSync('cancletotal',this.data.cancleOrder.length+res.data.data.historyorders.pageOrderList.length);
        this.setData({
          cancletotal: wx.getStorageSync('cancletotal'),
          cancletime: wx.getStorageSync('cancletime'),
          cancleOrder: wx.getStorageSync('cancleOrder'),
        })
        wx.stopPullDownRefresh();
        break;
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var status = this.data.currtab;
    switch(status){
      case 0:
        this.setData({
          finishOrder:[],
          finishtime:[]
        })
       case 1:
        this.setData({
          payOrder:[],
          paytime:[]
        })
        case 2:
          this.setData({
            cancleOrder:[],
            cancletime:[]
          })
    }
    this.QueryParams.pagenum=1;
    this.alreadyShow(status);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var status = this.data.currtab;
    if(this.QueryParams.pagenum>=this.totalPages){
      wx.showToast({
        title: '已经到底了',
      });
        
    }else{
      this.QueryParams.pagenum++;
      this.alreadyShow(status);
    }
  },   
  feedback(e){
    console.log(e);
    var oid = e.currentTarget.dataset.oid;
    wx.navigateTo({
      url: '/pages/feedback/feekback?oid='+oid,
     })
  }
})